<?php
namespace Admin\Controller;

class AreaGoodsController extends CommonController
{

    /**
     * 列表
     *
     * @param  void
     * @return void
     */
    public function index()
    {
        if (IS_POST) {
            $this->delete();
        } else {
            $get = I('get.');
            $where = array();
            if (! empty($get['sell_time'])) {
                $where['start_sell_time'] = array( 'egt' , strtotime("{$get['sell_time']} 00:00:00") );
                $where['end_sell_time'] = array( 'elt' , strtotime("{$get['sell_time']} 23:59:59") );
            }
            if (! empty($get['deliver_time'])) {
                $where['start_deliver_time'] = array( 'egt' , strtotime("{$get['deliver_time']} 00:00:00") );
                $where['end_deliver_time'] = array( 'elt' , strtotime("{$get['deliver_time']} 23:59:59") );
            }
            if (! empty($get['area_id'])) {
                $where['area_id'] = $get['area_id'];
            }
            $where['status'] = 1;
            $list = $this->lists(M('AreaGoods'), true, $where, 'id DESC', 10);
            $store = getStoreList(false);
            $area = getArea(false);
            $goods = getGoodsList(false);
            $this->assign('list', $list);
            $this->assign('store', $store);
            $this->assign('area', $area);
            $this->assign('goods', $goods);
            $this->assign('get', $get);
            $this->display();
        }
    }
    
    /**
     * 新增
     *
     * @param  void
     * @return void
     */
    public function add()
    {
        $goods = getGoodsList(false);
        if (IS_POST) {
            $data = I('post.');
            $data['start_sell_time'] = strtotime("{$data['start_sell_time']} {$data['start_sell_hour']}:{$data['start_sell_minute']}:00");
            $data['end_sell_time'] = strtotime("{$data['end_sell_time']} {$data['end_sell_hour']}:{$data['end_sell_minute']}:00");
            
            $data['start_deliver_time'] = strtotime("{$data['start_deliver_time']} {$data['start_deliver_hour']}:{$data['start_deliver_minute']}:00");
            $data['end_deliver_time'] = strtotime("{$data['end_deliver_time']} {$data['end_deliver_hour']}:{$data['end_deliver_minute']}:00");
            
            $data['store_id'] = $goods[$data['goods_id']]['store_id'];
            $model = D('AreaGoods');
            $result = $model->update($data);
            if (false !== $result) {
                $this->success('保存成功', U('index'));
            } else {
                $error = $model->getError();
                $this->error(empty($error) ? '未知错误' : $error);
            }
        } else {
            $area = getArea(false);
            $hour = C('HOUR');
            $minute = C('MINUTE');
            $store = getStoreList(false);
            $this->assign('store', $store);
            $this->assign('hour', $hour);
            $this->assign('minute', $minute);
            $this->assign('area', $area);
            $this->assign('goods', $goods);
            $this->display();
        }
    }

    /**
     * 修改
     *
     * @param void
     * @return void
     */
    public function edit()
    {
        $goods = getGoodsList(false);
        if (IS_POST) {
            $data = I('post.');
            $data['start_sell_time'] = strtotime("{$data['start_sell_time']} {$data['start_sell_hour']}:{$data['start_sell_minute']}:00");
            $data['end_sell_time'] = strtotime("{$data['end_sell_time']} {$data['end_sell_hour']}:{$data['end_sell_minute']}:00");
            
            $data['start_deliver_time'] = strtotime("{$data['start_deliver_time']} {$data['start_deliver_hour']}:{$data['start_deliver_minute']}:00");
            $data['end_deliver_time'] = strtotime("{$data['end_deliver_time']} {$data['end_deliver_hour']}:{$data['end_deliver_minute']}:00");
            
            $data['store_id'] = $goods[$data['goods_id']]['store_id'];
            $model = D('AreaGoods');
            $result = $model->update($data);
            if (false !== $result) {
                $this->success('保存成功', U('index'));
            } else {
                $error = $model->getError();
                $this->error(empty($error) ? '未知错误' : $error);
            }
        } else {
            $id = I('get.id');
            if (empty($id)) {
                $this->error('无效的数据');
            }
            $info = M('AreaGoods')->where('id = ' . $id)->find();
            $area = getArea(false);
            $hour = C('HOUR');
            $minute = C('MINUTE');
            $store = getStoreList(false);
            $this->assign('store', $store);
            $this->assign('hour', $hour);
            $this->assign('minute', $minute);
            $this->assign('area', $area);
            $this->assign('goods', $goods);
            $this->assign('info', $info);
            $this->display();
        }
    }

    /**
     * 删除
     *
     * @param void
     * @return void
     */
    public function delete()
    {
        $id = array_unique((array) I('id', 0));
        if (empty($id)) {
            $this->error('请选择要操作的数据!');
        }
        $where = array( 'id' => array( 'in', $id ) );
        $data = array( 'status' => 0, 'update_time' => time() );
        $result = M('AreaGoods')->where($where)->save($data);
        if ($result) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }  
    
}
